<?php
session_start();
function func_response($str = '')
{
    echo $str;exit;
}
function func_get($str){
    return isset($_GET[$str]) ? $_GET[$str] : '';
}
function func_request($str){
    return isset($_REQUEST[$str]) ? $_REQUEST[$str] : '';
}
function func_post($str){
    return isset($_POST[$str]) ? $_POST[$str] : '';
}
$t = func_request('t');
if ($t == '') {
    func_response('t不能为空error');
}
if ($t == 'out') {
    $_SESSION['u_id'] = null;
    func_response('退出成功');
}

require('db.php');

if ($t != 'login') {
    $u_id = $_SESSION['u_id'];
    if(empty($u_id)) {
        $_SESSION['u_id'] = null;
        func_response("你未登录");
        exit;
    }
    $sql = "select id,is_admin,`use` from lyl_users where id=" . $u_id;
    $user = Database::query($sql);
    if ($user) {
        $is_admin = 0;
        if($user[0]['is_admin']) {
            $is_admin = 1;
        } else if($user[0]['use'] == 0){
            $_SESSION['u_id'] = null;
            func_response("你的账户被禁用");
        }
    } else {
        $_SESSION['u_id'] = null;
        func_response("你未登录");
        exit;
    }
}
if(in_array($t,['add_user','add_sing','1','2','4','5','6','7']) && $is_admin == 0) {
    func_response("你不是管理员，不能操作");
}

if ($t == 'login') {
    $u_name = func_request('u_name');
    $u_pwd = func_request('u_pwd');
    if(empty($u_name)) {
        func_response('账号不能为空');
    }
    if(empty($u_name)) {
        func_response('密码不能为空');
    }
    $sql = "SELECT * FROM lyl_users where u_pwd='{$u_pwd}' and u_name='{$u_name}' LIMIT 1 ";
    $rs = Database::query($sql);
    if ($rs) {
        if($rs[0]['use'] == 0){
            $_SESSION['u_id'] = null;
            func_response("你的账户被禁用");
        }
        $_SESSION['u_id'] = $rs[0]['id'];
        func_response('登录成功');
    } else {
        func_response('账号密码错误，重新登录');
    }
}
if ($t == 'add_user') {

    $f_name = func_request('f_name');
    $u_name = func_request('u_name');
    $u_pwd = func_request('u_pwd');
    if(empty($f_name)) {
        func_response('姓名不能为空');
    }
    if(empty($u_name)) {
        func_response('账号不能为空');
    }
    if(empty($u_pwd)) {
        func_response('密码不能为空');
    }
    $sql = "SELECT * FROM lyl_users where f_name='{$f_name}' and u_name='{$u_name}' LIMIT 1 ";
    $rs = Database::query($sql);
    if ($rs) {
        func_response('不能重复添加！');
    } else {
        $up = "INSERT INTO lyl_users(f_name,u_name,u_pwd,`time`)VALUES ('{$f_name}', '{$u_name}','{$u_pwd}',now())";
        $rs = Database::change($up);
        if($rs) {
            func_response('添加成功');
        } else {
            func_response('添加失败');
        }
    }
}


if ($t == 'add_sing') {

    $name = func_request('name');
    $code = func_request('code');
    $type = func_request('type');
    $num = func_request('num');
    $price = func_request('price');
    if(empty($name)) {
        func_response('名称不能为空');
    }
    if(empty($code)) {
        func_response('编号不能为空');
    }
    if(empty($type)) {
        func_response('类型不能为空');
    }
    if(!is_numeric($num) || $num < 1) {
        func_response('总数不正确');
    }
    if(empty($price)) {
        func_response('总数不正确');
    }
    $sql = "SELECT * FROM lyl_singleton where name='{$name}' and code='{$code}' and type='{$type}' LIMIT 1 ";
    $rs = Database::query($sql);
    if (count($rs) > 0) {
        func_response('不能重复添加！');
    } else {
        $up = "INSERT INTO `lyl_singleton` (`name`,`code`,`type`,`num`,`price`,`time`)VALUES ('{$name}', '{$code}','{$type}',{$num},{$price},now())";
        $rs = Database::change($up);
        if($rs > 0) {
            func_response('添加成功');
        } else {
            func_response('添加失败');
        }
    }
}
if ($t == '1') { //隐藏显示 信息
    $use = func_request('use');
    $id = func_request('id');
    if(!in_array($use,[0,1])) {
        $use = 1;
    }

    $up = "UPDATE `lyl_singleton` SET `use`={$use} where id={$id}";
    $rs = Database::change($up);
    if($rs) {
        func_response('成功');
    } else {
        func_response('失败');
    }
}

if ($t == '2') { //编辑信息
    $id = func_request('id');
    $name = func_request('name');
    $code = func_request('code');
    $type = func_request('type');
    $num = func_request('num');
    $price = func_request('price');
    if(empty($name)) {
        func_response('名称不能为空');
    }
    if(empty($code)) {
        func_response('编号不能为空');
    }
    if(empty($type)) {
        func_response('类型不能为空');
    }
    if(!is_numeric($num) || $num < 1) {
        func_response('总数不正确');
    }
    if(empty($price)) {
        func_response('总数不正确');
    }
    $up = "UPDATE `lyl_singleton` SET `name`='{$name}',`code`='{$code}',`type`='{$type}',`num`='{$num}',`price`='{$price}' where id={$id}";
    $rs = Database::change($up);
    if($rs > 0) {
        func_response('编辑成功');
    } else {
        func_response('编辑失败');
    }

}

if ($t == '3') { //用户填单子
    $up = '';
    $id = func_request('id');
    $num = func_request('num');
    if(!is_numeric($num) || $num < 0) {
        func_response('数量不正确');
    }
    $sql = "SELECT * FROM lyl_bill where s_id={$id} and u_id='{$u_id}' LIMIT 1 ";
    $rs = Database::query($sql);
    $old_num = 0;
    $old_id = 0;
    if(count($rs) > 0) {
        $old_id = $rs[0]['id'];
        $old_num = $rs[0]['num'];
    }
    $sql = "SELECT * FROM lyl_singleton where id='{$id}' LIMIT 1 ";
    $rs = Database::query($sql);
    if(count($rs) > 0) {
        $all_num = $rs[0]['num'];
        $all_n = $rs[0]['n'];
        if($old_id > 0) { //更新
            if($old_num == $num) {
                func_response('数量没变化，无需修改');
            }
            if($old_num > $num) { // 变少了
                if($num == 0) {
                    $up = "DELETE FROM `lyl_bill` where u_id={$u_id} and s_id={$id}";
                } else {
                    $up = "UPDATE `lyl_bill` SET `num`={$num} where u_id={$u_id} and s_id={$id}";
                }
                $rs = Database::change($up);
                if ($rs <= 0) {
                    func_response('失败');
                }
                $up = "UPDATE `lyl_singleton` SET `n`=".($all_n - ($old_num - $num))." where `id`={$id}";
                $rs = Database::change($up);
                if ($rs > 0) {
                    func_response('成功');
                } else {
                    func_response('失败');
                }
            } else {
                if(($num - $old_num + $all_n) > $all_num) {
                    func_response('失败，当前你已填写'.$old_num.'，最多可填' .($old_num + $all_num - $all_n). "个");
                }
                $up = "UPDATE `lyl_bill` SET `num`={$num} where u_id={$u_id} and s_id={$id}";
                $rs = Database::change($up);
                if ($rs <= 0) {
                    func_response('失败');
                }
                $up = "UPDATE `lyl_singleton` SET `n`=".($all_n + ($num - $old_num))." where `id`={$id}";
                $rs = Database::change($up);
                if ($rs > 0) {
                    func_response('成功');
                } else {
                    func_response('失败');
                }
            }
        } else {
            if(($num + $all_n) > $all_num) {
                func_response('失败，最多可填' .($all_num - $all_n). "个");
            }
            if($num == 0) {
                func_response('必须大于0');
            }
            $up = "INSERT INTO `lyl_bill` (`u_id`,`s_id`,`num`,`time`)VALUES ({$u_id}, {$id},{$num},now())";
            $rs = Database::change($up);
            if ($rs <= 0) {
                func_response('失败');
            }
            $up = "UPDATE `lyl_singleton` SET `n`=".($all_n + $num)." where `id`={$id}";
            $rs = Database::change($up);
            if ($rs > 0) {
                func_response('成功');
            } else {
                func_response('失败');
            }
        }
    } else {
        func_response('没有该信息，不能填写');
    }
}

if ($t == '4') { //隐藏显示 信息
    $use = func_request('use');
    $id = func_request('id');
    if(!in_array($use,[0,1])) {
        $use = 1;
    }
    $up = "UPDATE `lyl_users` SET `use`={$use} where id={$id}";
    $rs = Database::change($up);
    if($rs) {
        func_response('成功');
    } else {
        func_response('失败');
    }
}

if ($t == '5') { //编辑信息
    $id = func_request('id');
    $f_name = func_request('f_name');
    $u_name = func_request('u_name');
    $u_pwd = func_request('u_pwd');
    if(empty($f_name)) {
        func_response('姓名不能为空');
    }
    if(empty($u_name)) {
        func_response('账号不能为空');
    }
    if(empty($u_pwd)) {
        func_response('密码不能为空');
    }
    $sql = "SELECT * FROM lyl_users where f_name='{$f_name}' and u_name='{$u_name}' and id<>{$id} LIMIT 1 ";
    $rs = Database::query($sql);
    if ($rs) {
        func_response('不能修改成相同！');
    } else {
        $up = "UPDATE `lyl_users` SET `f_name`='{$f_name}',`u_name`='{$u_name}',`u_pwd`='{$u_pwd}' where id={$id}";
        $rs = Database::change($up);
        if($rs > 0) {
            func_response('编辑成功');
        } else {
            func_response('编辑失败');
        }
    }
}

if ($t == '6') { //删除
    $id = func_request('id');
    if(!is_numeric($id) || $id <= 0) {
        func_response('非法参数');
    }
    $sql = "SELECT * FROM lyl_bill where u_id='{$id}' LIMIT 1 ";
    $rs = Database::query($sql);
    if(count($rs) > 0) {
        func_response('该账号已填单过，不能删除');
    }
    $up = "DELETE FROM `lyl_users` WHERE id={$id}";
    $rs = Database::change($up);
    if($rs) {
        func_response('成功');
    } else {
        func_response('失败');
    }
}

if ($t == '7') {
    $content = func_request('content');
    $arr = explode(PHP_EOL, $content);
    $i = 0;
    foreach ($arr as $item) {
        $info = explode('----', $item);
        $f_name = $info[0];
        $u_name = $info[1];
        $u_pwd = $info[2];
        if(empty($f_name) || empty($u_name) || empty($u_pwd)) {
            continue;
        }
        $sql = "SELECT * FROM lyl_users where f_name='{$f_name}' and u_name='{$u_name}' LIMIT 1 ";
        $rs = Database::query($sql);
        if ($rs) {
            continue;
        }
        $up = "INSERT INTO lyl_users(f_name,u_name,u_pwd,`time`)VALUES ('{$f_name}', '{$u_name}','{$u_pwd}',now())";
        $rs = Database::change($up);
        if($rs > 0) {
            $i++;
        }
    }
    func_response('成功添加' . $i . "个");
}
exit;
